#include "stdafx.h"
#include "BigInt.h"

using namespace System;
using namespace System::Text;
using namespace System::Collections::Generic;
using namespace Microsoft::VisualStudio::TestTools::UnitTesting;
using namespace APIAL;

namespace BigIntUnitTest
{
	[TestClass]
	public ref class AddUnitTest
	{
	private:
		TestContext^ testContextInstance;
		static BigInt *op1;
		static BigInt *op2;
		static BigInt *scratch1;
		static BigInt *result1;
		static BigInt *result2;
		static BigInt *expected;

	public: 
		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		property Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ TestContext
		{
			Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ get()
			{
				return testContextInstance;
			}
			System::Void set(Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ value)
			{
				testContextInstance = value;
			}
		};

		#pragma region Additional test attributes
		//
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		[ClassInitialize()]
		static void MyClassInitialize(Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ testContext) {
			op1 = new BigInt(20, 8);
			op2 = new BigInt(20, 8);
			result1 = new BigInt(20, 8);
			result2 = new BigInt(20, 8);
			scratch1 = new BigInt(20, 8);
			expected = new BigInt(20, 8);
		}

		//Use ClassCleanup to run code after all tests in a class have run
		[ClassCleanup()]
		static void MyClassCleanup() { 
			delete op1;
			delete op2;
			delete result1;
			delete result2;
			delete scratch1;
			delete expected;
		}

		//
		//Use TestInitialize to run code before running each test
		[TestInitialize()]
		void MyTestInitialize() {
			//op1->clear();
			//op2->clear();
			//result1->clear();
			//result2->clear();
			//scratch1->clear();
			//expected->clear();
		}

		//
		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//void MyTestCleanup() {};
		//
		#pragma endregion 

		[TestMethod]
		void TestAddNoOverflow()
		{
			op1->chunks[0] = 0x7F;
			op1->chunks[1] = 0x04;
			op1->chunks[2] = 0xFF;
			op1->normalized = true;
			op1->size = 2;
			op1->positive = true;

			op2->chunks[0] = 0x01;
			op2->chunks[1] = 0x10;
			op2->chunks[2] = 0xFF;
			op2->normalized = true;
			op2->size = 2;
			op2->positive = true;

			result1->chunks[0] = 0;
			result1->chunks[1] = 0;
			result1->chunks[2] = 0;
			result1->size = 0;
			
			Add(op1, op2, result1, scratch1);
			Assert::AreEqual<BigInt::chunkType>(0x01+0x7F, result1->chunks[0]);
			Assert::AreEqual<BigInt::chunkType>(0x04+0x10, result1->chunks[1]);
			Assert::AreEqual<BigInt::chunkType>(0x0, result1->chunks[2]);
			Assert::AreEqual<int>(2, result1->size);
		};

		[TestMethod]
		void TestAddOverflow() {
			op1->chunks[0] = 0xFF;
			op1->chunks[1] = 0x04;
			op1->chunks[2] = 0xFF;
			op1->normalized = false;
			op1->size = 2;
			op1->positive = true;

			op2->chunks[0] = 0x81;
			op2->chunks[1] = 0x90;
			op2->chunks[2] = 0xFF;
			op2->normalized = false;
			op2->size = 2;
			op2->positive = true;

			result1->chunks[0] = 0;
			result1->chunks[1] = 0;
			result1->chunks[2] = 0;
			result1->size = 0;

			Add(op1, op2, result1, scratch1);
			Assert::AreEqual<BigInt::chunkType>(0x00, result1->chunks[0]);
			Assert::AreEqual<BigInt::chunkType>(0x04+0x10+0x03, result1->chunks[1]);
			Assert::AreEqual<BigInt::chunkType>(0x01, result1->chunks[2]);
			Assert::AreEqual<int>(3, result1->size);
		}

		[TestMethod]
		void TestAdd() {
			op1->clear();
			op2->clear();
			op1->ParseHexadecimalString("A92B1FEC21858235", true);
			op2->ParseHexadecimalString("98721785EBAC2F", true);
			for (int i = op1->size; i < op1->capacity; i++) {
				op1->chunks[i] = ~0;
			}
			for (int i = op2->size; i < op2->capacity; i++) {
				op2->chunks[i] = ~0;
			}
			
			// Testing hexadecimal parsing briefly...
			result1->clear();
			result1->ParseHexadecimalString("A92B1FEC21858235", true);
			result2->clear();
			result2->ParseHexadecimalString("98721785EBAC2F", true);
			for (int i = 0; i < result1->size; i++) {
				Assert::AreEqual<BigInt::chunkType>(result1->chunks[i], op1->chunks[i], 
					System::String::Format("Failed at index {0:d} out of {1:d}", i, result1->size));
			}
			for (int i = 0; i < result2->size; i++) {
				Assert::AreEqual<BigInt::chunkType>(result2->chunks[i], op2->chunks[i], 
					System::String::Format("Failed at index {0:d} out of {1:d}", i, result1->size));
			}

			expected->ParseHexadecimalString("A9C39203A7712E64", true);
			Add(op1, op2, result1, scratch1);
			BigInt::normalize(*result1);
			for (int i = 0; i < expected->size; i++) {
				Assert::AreEqual<BigInt::chunkType>(expected->chunks[i], result1->chunks[i], 
					System::String::Format("Failed at index {0:d} out of {1:d}", i, expected->size));
			}
		}

		[TestMethod]
		void TestAddMultipleSimple() {
			op1->ParseHexadecimalString("FE", true);
			op2->ParseHexadecimalString("A7", true);
			Add(op1, op2, result1, scratch1);
			Add(op1, result1, result2, scratch1);
			BigInt::normalize(*result2);
			expected->ParseHexadecimalString("2A3", true);
			for (int i = 0; i < expected->size; i++) {
				Assert::AreEqual<BigInt::chunkType>(expected->chunks[i], result2->chunks[i]);
			}
		}

		[TestMethod]
		void TestAddMultiple() {
			op1->ParseHexadecimalString("A92B1FEC21858235", true);
			op2->ParseHexadecimalString("98721785EBAC2F", true);
			Add(op1, op2, result1, scratch1);
			Add(op1, result1, result2, scratch1);
			expected->ParseHexadecimalString("52EEB1EFC8F6B099", true);
			BigInt::normalize(*result2);
			for (int i = 0; i < expected->size; i++) {
				Assert::AreEqual<BigInt::chunkType>(expected->chunks[i], result2->chunks[i], 
					System::String::Format("Failed at index {0:d} out of {1:d}", i, expected->size));
			}
		}

		[TestMethod]
		void TestAddAliasLeft() {
			op1->ParseHexadecimalString("A92B1FEC21858235", true);
			op2->ParseHexadecimalString("98721785EBAC2F", true);
			expected->ParseHexadecimalString("A9C39203A7712E64", true);
			Add(op1, op2, op1, scratch1);
			BigInt::normalize(*op1);
			for (int i = 0; i < expected->size; i++) {
				Assert::AreEqual<BigInt::chunkType>(expected->chunks[i], op1->chunks[i],
					System::String::Format("Failed at index {0:d} out of {1:d}", i, expected->size));
			}
		}

		[TestMethod]
		void TestAddAliasRight() {
			op1->ParseHexadecimalString("A92B1FEC21858235", true);
			op2->ParseHexadecimalString("98721785EBAC2F", true);
			expected->ParseHexadecimalString("A9C39203A7712E64", true);
			Add(op1, op2, op2, scratch1);
			BigInt::normalize(*op2);
			for (int i = 0; i < expected->size; i++) {
				Assert::AreEqual<BigInt::chunkType>(expected->chunks[i], op2->chunks[i], 
					System::String::Format("Mismatch at {0:d} out of {1:d}", i, expected->size));
			}
		}

		[TestMethod]
		void TestNormalize() {
			op1->chunks[0] = 0xFF;
			op1->chunks[1] = 0xFF;
			op1->chunks[2] = 0xFF;
			op1->chunks[3] = 0;
			op1->normalized = false;
			op1->size = 3;
			op1->positive = true;
			BigInt::normalize(*op1);
			Assert::AreEqual<int>(4, op1->size);
			Assert::AreEqual<BigInt::chunkType>(0x7F, op1->chunks[0]);
			Assert::AreEqual<BigInt::chunkType>(0x00, op1->chunks[1]);
			Assert::AreEqual<BigInt::chunkType>(0x01, op1->chunks[2]);
			Assert::AreEqual<BigInt::chunkType>(0x02, op1->chunks[3]);
		}

		[TestMethod]
		void TestAddDifferingSigns() {
			op1->ParseHexadecimalString("235987235", true);
			op2->ParseHexadecimalString("93857BCE", false);
			expected->ParseHexadecimalString("1A212F667", true);

			// Test (left > right, pos+neg)
			Add(op1, op2, result1, scratch1);
			Assert::IsTrue(op1->positive);
			Assert::IsFalse(op2->positive);
			Assert::IsTrue(Equal(result1, expected));

			// Test (right > left, pos+neg)
			op2->positive = true;
			op1->positive = false;
			expected->positive = false;
			Add(op2, op1, result1, scratch1);
			Assert::IsTrue(op2->positive);
			Assert::IsFalse(op1->positive);
			Assert::IsTrue(Equal(result1, expected));

			// Test (left > right, neg+pos)
			op1->positive = false;
			op2->positive = true;
			expected->positive = false;
			Add(op1, op2, result1, scratch1);
			Assert::IsFalse(op1->positive);
			Assert::IsTrue(op2->positive);
			Assert::IsTrue(Equal(result1, expected));

			// Test (right > left, neg+pos)
			op2->positive = false;
			op1->positive = true;
			expected->positive = true;
			Add(op2, op1, result1, scratch1);
			Assert::IsFalse(op2->positive);
			Assert::IsTrue(op1->positive);
			Assert::IsTrue(Equal(result1, expected));

			// Test (left > right, pos+pos)
			op1->positive = true;
			op2->positive = true;
			expected->ParseHexadecimalString("2C91DEE03", true);
			Add(op1, op2, result1, scratch1);
			Assert::IsTrue(op1->positive);
			Assert::IsTrue(op2->positive);
			Assert::IsTrue(Equal(result1, expected));

			// Test (right > left, pos+pos)
			expected->positive = true;
			Add(op2, op1, result1, scratch1);
			Assert::IsTrue(op1->positive);
			Assert::IsTrue(op2->positive);
			Assert::IsTrue(Equal(result1, expected));

			// Test (left > right, neg+neg)
			op1->positive = false;
			op2->positive = false;
			expected->positive = false;
			Add(op1, op2, result1, scratch1);
			Assert::IsFalse(op1->positive);
			Assert::IsFalse(op2->positive);
			Assert::IsTrue(Equal(result1, expected));

			// Test (right > left, neg+neg)
			expected->positive = false;
			Add(op2, op1, result1, scratch1);
			Assert::IsFalse(op1->positive);
			Assert::IsFalse(op2->positive);
			Assert::IsTrue(Equal(result1, expected));
		}
	};
}
